package com.pangda.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.common.utils.FileUtil;
import com.pangda.common.utils.UserContext;
import com.pangda.pojo.DictData;
import com.pangda.pojo.data.DictDatas;
import com.pangda.pojo.dto.DictDataDto;
import com.pangda.pojo.dto.DictPageParam;
import com.pangda.pojo.vo.DictDataVo;
import com.pangda.service.IDictDataService;
import com.pangda.service.IDictTypeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author Panda.No72
 * @since 2024-06-20
 */
@RestController
@RequestMapping("/system/dict/data")
@Api(tags = "字典数据")
public class DictDataController {
    @Autowired
    private IDictDataService dictDataService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 分页条件查询
     * @param param
     * @return
     */
    @GetMapping("/list")
    public PageResult<DictDataVo> list(DictPageParam param) {
        return dictDataService.getList(param);
    }

    /**
     * 新增字典数据
     *
     * @param dictDataDto
     * @return
     */
    @PostMapping
    public ResponseResult add(@RequestBody DictDataDto dictDataDto) {
        return dictDataService.add(dictDataDto);
    }

    /**
     * 根据字典类型查询字典数据信息
     *
     * @param dictType
     * @return
     */
    @GetMapping("/type/{dictType}")
    public ResponseResult getByType(@PathVariable String dictType) {
        List<DictDataVo> dictDataVos = BeanUtil.copyToList(dictDataService.list(new LambdaQueryWrapper<DictData>().eq(DictData::getDictType, dictType)), DictDataVo.class);
        return ResponseResult.success(dictDataVos);
    }

    /**
     * 修改字典数据
     *
     * @param dictDataDto
     * @return
     */
    @PutMapping
    public ResponseResult update(@RequestBody DictDataDto dictDataDto) {
        dictDataDto.setUpdateTime(LocalDateTime.now());
        dictDataDto.setUpdateBy(UserContext.getUser());
        DictData dictData = BeanUtil.toBean(dictDataDto, DictData.class);
        return ResponseResult.success(dictDataService.updateById(dictData));
    }

    /**
     * 根据字典编码查询字典数据
     * @param dictCode
     * @return
     */
    @GetMapping("/{dictCode}")
    public ResponseResult getInfo(@PathVariable Integer dictCode) {
        return ResponseResult.success(dictDataService.getById(dictCode));
    }

    /**
     * 删除字典数据
     * @param dictCodes
     * @return
     */
    @DeleteMapping("/{dictCodes}")
    public ResponseResult remove(@PathVariable List<Integer> dictCodes) {
        return ResponseResult.success(dictDataService.removeByIds(dictCodes));
    }

    @GetMapping("/export")
    public Map<String,String> export(DictPageParam param) throws IOException {
        LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictData::getDictType, param.getDictType());
        List<DictData> list = dictDataService.list(wrapper);
        List<DictDatas> dictDatas = BeanUtil.copyToList(list, DictDatas.class);
        return FileUtil.uploadFile(dictDatas, DictDatas.class, redisTemplate);
    }

}
